package com.ycl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.common.CustomException;
import com.ycl.dto.SetmealDto;
import com.ycl.entity.Category;
import com.ycl.entity.Setmeal;
import com.ycl.entity.SetmealDish;
import com.ycl.mapper.SetmealMapper;
import com.ycl.service.CategoryService;
import com.ycl.service.SetmealDishService;
import com.ycl.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    SetmealDishService setmealDishService;
    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        // 保存套餐的基本信息，操作stmeal表
        this.save(setmealDto);

        // 保存套餐和菜品的关联信息，操作setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐 C
     * @param ids
     */
    @Override
    public void deleteWithDish(List<Long> ids) {
        // select count(*) from setmeal where id in (id1,id2,id3...) and status
        // 查询套餐状态是否可删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .in(Setmeal::getId,ids)
                .eq(Setmeal::getStatus,1);
        long count = this.count(queryWrapper);
        // 如果不能删除，则抛出一个业务异常
        if (count > 0){
            throw new CustomException("套餐售卖中，不能删除");
        }

        // 如果可以删除，删除套餐表中的数据--setmeal
        this.removeByIds(ids);

        // 删除关系表中的数据
        // delete from setmeal_dish where getSetmealId in (1,2,3...)
        LambdaQueryWrapper<SetmealDish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.in(SetmealDish::getSetmealId,ids);
        // 删除关系表中的数据setmeal_dish
        setmealDishService.remove(dishQueryWrapper);

    }

    /**
     * 跟据ID获取套餐setmeal和套餐菜品setmealDish，用于回显
     * @param id
     */
    @Override
    public SetmealDto getWithDish(Long id) {
        // 获取套餐
        Setmeal setmeal = this.getById(id);

        // 获取套餐关联菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        // 创建返回SetmealDto类
        SetmealDto setmealDto = new SetmealDto();

        // 将查询数据塞入setmealDto
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        // 解决修改后套餐变为起售
        Long setmealDtoId = setmealDto.getId();
        Setmeal setmeal = this.getById(setmealDtoId);
        Integer status = setmeal.getStatus();
        setmealDto.setStatus(status);

        // 更新基本套餐setmeal表信息
        this.updateById(setmealDto);
        // 删除套餐关联菜品的信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        // 更新套餐关联菜品setmeal_dish表信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes().stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
}
